{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [],
   "source": [
    "from selenium import webdriver\n",
    "from selenium.webdriver.common.by import By\n",
    "from selenium.webdriver.support.ui import Select\n",
    "from selenium.webdriver.common.action_chains import ActionChains\n",
    "import time"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Webdriver:\n",
    "- [Chrome](https://chromedriver.storage.googleapis.com/index.html)\n",
    "- [Edge](https://developer.microsoft.com/zh-cn/microsoft-edge/tools/webdriver/#downloads)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 创建一个wb实例"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\Handuoduo\\AppData\\Local\\Temp\\ipykernel_19620\\2396472819.py:1: DeprecationWarning: executable_path has been deprecated, please pass in a Service object\n",
      "  wd = webdriver.Chrome(r'chromedriver.exe')\n"
     ]
    }
   ],
   "source": [
    "wd = webdriver.Chrome(r'chromedriver.exe')\n",
    "\n",
    "# 访问网站\n",
    "wd.get(r'https://www.baidu.com/')\n",
    "\n",
    "# 根据元素的id值选择，返回的是该元素对应的Welement对象\n",
    "\n",
    "elmt_kuang = wd.find_element(By.ID,'kw') #得到一个输入框\n",
    "elmt_bottum = wd.find_element(By.ID,'su') #得到一个按钮\n",
    "\n",
    "elmt_kuang.send_keys('英雄联盟') # 输入英雄联盟\n",
    "elmt_bottum.click() #点击百度一下"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 通过Webdriver 对象选择元素"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "```\n",
    "<body>\n",
    "\n",
    "    <div class=\"plant\"><span>土豆</span></div>\n",
    "    <div class=\"plant\"><span>洋葱</span></div>\n",
    "    <div class=\"plant\"><span>白菜</span></div>\n",
    "    <div class=\"plant\"><span>青椒</span></div>\n",
    "    \n",
    "    <div class=\"animal\"><span>狮子</span></div>\n",
    "    <div class=\"animal\"><span>老虎</span></div>\n",
    "    <div class=\"animal\"><span>大象</span></div>\n",
    "\n",
    "</body>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 根据class查找"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "如果我们要选择**所有的动物**，就可以使用方法 `find_elements_by_class_name`\n",
    "\n",
    "注意后面多了s\n",
    "\n",
    "找到符合的元素对象放到列表中\n",
    "\n",
    "如果不加 s 默认返回第一个"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\Handuoduo\\AppData\\Local\\Temp\\ipykernel_19620\\2399046086.py:2: DeprecationWarning: executable_path has been deprecated, please pass in a Service object\n",
      "  wd = webdriver.Chrome(r'chromedriver.exe')\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[<selenium.webdriver.remote.webelement.WebElement (session=\"0978e62f66fbf2ebc3d15ef28d765689\", element=\"a99c6ce4-0c2d-4fa5-86ab-2ac051d93efb\")>, <selenium.webdriver.remote.webelement.WebElement (session=\"0978e62f66fbf2ebc3d15ef28d765689\", element=\"80efaf73-9a8d-4bd3-933f-78312a807ce3\")>, <selenium.webdriver.remote.webelement.WebElement (session=\"0978e62f66fbf2ebc3d15ef28d765689\", element=\"a1101d28-6b69-44f0-bbfc-dcb670182870\")>]\n",
      "狮子\n",
      "老虎\n",
      "山羊\n"
     ]
    }
   ],
   "source": [
    "url = r'http://cdn1.python3.vip/files/selenium/sample1.html'\n",
    "wd = webdriver.Chrome(r'chromedriver.exe')\n",
    "wd.get(url)\n",
    "elements = wd.find_elements(By.CLASS_NAME,'animal') # 返回element对象列表\n",
    "print(elements)\n",
    "for element in elements:\n",
    "    print(element.text)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 根据标签查找，例：找出所有span标签"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\Handuoduo\\AppData\\Local\\Temp\\ipykernel_19620\\4281764511.py:2: DeprecationWarning: executable_path has been deprecated, please pass in a Service object\n",
      "  wd = webdriver.Chrome(r'chromedriver.exe')\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "土豆\n",
      "洋葱\n",
      "白菜\n",
      "狮子\n",
      "老虎\n",
      "山羊\n",
      "内层11\n",
      "内层12\n",
      "内层21\n",
      "版权\n",
      "发布日期：2018-03-03\n",
      "备案号 苏ICP备88885574号\n"
     ]
    }
   ],
   "source": [
    "url = r'http://cdn1.python3.vip/files/selenium/sample1.html'\n",
    "wd = webdriver.Chrome(r'chromedriver.exe')\n",
    "wd.get(url)\n",
    "\n",
    "elements = wd.find_elements(By.TAG_NAME,'span') # 返回element对象列表\n",
    "for element in elements:\n",
    "    print(element.text)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 通过Webelement对象选择"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- WebDriver 对象 选择元素的范围是整个web页面\n",
    "- WebElement 对象 选择元素的范围是改元素的内部"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\Handuoduo\\AppData\\Local\\Temp\\ipykernel_19620\\557734964.py:2: DeprecationWarning: executable_path has been deprecated, please pass in a Service object\n",
      "  wd = webdriver.Chrome(r'chromedriver.exe')\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "内层11\n",
      "内层12\n",
      "内层21\n"
     ]
    }
   ],
   "source": [
    "url = r'http://cdn1.python3.vip/files/selenium/sample1.html'\n",
    "wd = webdriver.Chrome(r'chromedriver.exe')\n",
    "wd.get(url)\n",
    "\n",
    "element = wd.find_element(By.ID,'container')\n",
    "\n",
    "spans = element.find_elements(By.TAG_NAME,'span')\n",
    "\n",
    "for span in spans:\n",
    "    print(span.text)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 智能等待"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\Handuoduo\\AppData\\Local\\Temp\\ipykernel_19620\\195445409.py:2: DeprecationWarning: executable_path has been deprecated, please pass in a Service object\n",
      "  wd = webdriver.Chrome(r'chromedriver.exe')\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "能在一起 就是胜利-英雄联盟官方网站-腾讯游戏 官方\n",
      "英雄联盟官方网站,海量风格各异的英雄,丰富、便捷的物品合成系统,游戏内置的匹配、排行和竞技系统,独创的“召唤师”系统及技能、符文、天赋等系统组合,必将带你进入一个崭新而又丰富多彩的游戏世...\n",
      "英雄联盟百度快照\n"
     ]
    }
   ],
   "source": [
    "#实例化对象\n",
    "wd = webdriver.Chrome(r'chromedriver.exe')\n",
    "\n",
    "# 访问网站\n",
    "wd.get(r'https://www.baidu.com/')\n",
    "wd.implicitly_wait(10)# 智能等待\n",
    "\n",
    "\n",
    "# 搜索英雄联盟并点击\n",
    "wd.find_element(By.ID,'kw').send_keys('英雄联盟') #\n",
    "wd.find_element(By.ID,'su').click()\n",
    "\n",
    "#获取搜索的第一个结果的文本\n",
    "data = wd.find_element(By.ID,'1')\n",
    "print(data.text)\n",
    "\n",
    "# 退出程序\n",
    "wd.quit()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 操作元素\n",
    "- 输入：send_keys('xxx')\n",
    "- 点击：click()\n",
    "- 获取文本：text\n",
    "- 获取元素属性值：get_attribute('li') #获取li的属性值\n",
    "- 获取元素的整个html：get_attribute('outerHTML') #获取选择元素的整个HTML"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### CSS表达式"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 根据class查找 .xxx\n",
    "- 如果不加 . 则是根据标签名查找"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\Handuoduo\\AppData\\Local\\Temp\\ipykernel_19620\\1628794905.py:2: DeprecationWarning: executable_path has been deprecated, please pass in a Service object\n",
      "  wd = webdriver.Chrome(r'chromedriver.exe')\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "土豆\n"
     ]
    }
   ],
   "source": [
    "url = r'http://cdn1.python3.vip/files/selenium/sample1.html'\n",
    "wd = webdriver.Chrome(r'chromedriver.exe')\n",
    "wd.implicitly_wait(3)# 智能等待\n",
    "wd.get(url)\n",
    "\n",
    "element = wd.find_element(By.CSS_SELECTOR,'.plant') # 选择 class 为 plant 的元素\n",
    "\n",
    "print(element.text)\n",
    "\n",
    "wd.quit()\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 根据id查找  #+id"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\Handuoduo\\AppData\\Local\\Temp\\ipykernel_19620\\916076842.py:2: DeprecationWarning: executable_path has been deprecated, please pass in a Service object\n",
      "  wd = webdriver.Chrome(r'chromedriver.exe')\n"
     ]
    }
   ],
   "source": [
    "url = r'http://cdn1.python3.vip/files/selenium/sample1.html'\n",
    "wd = webdriver.Chrome(r'chromedriver.exe')\n",
    "wd.implicitly_wait(3)# 智能等待\n",
    "wd.get(url)\n",
    "\n",
    "\n",
    "\n",
    "element = wd.find_element(By.CSS_SELECTOR,'#searchtext') # 选择id 为 searchtext 的元素\n",
    "\n",
    "\n",
    "element.send_keys('定位到你了')\n",
    "\n",
    "\n",
    "\n",
    "time.sleep(3)\n",
    "wd.quit()\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 元素定位\n",
    "\n",
    "'>' 表示直接子元素\n",
    "\n",
    "元素1 > 目标元素 \n",
    "\n",
    "元素1 > 元素2 > 目标元素\n",
    "\n",
    "\n",
    "' ' 表示后代元素\n",
    "\n",
    "元素1  目标元素\n",
    "\n",
    "元素1    元素2     元素3 > 目标元素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\Handuoduo\\AppData\\Local\\Temp\\ipykernel_19620\\2005704176.py:2: DeprecationWarning: executable_path has been deprecated, please pass in a Service object\n",
      "  wd = webdriver.Chrome(r'chromedriver.exe')\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "内层11\n",
      "内层12\n",
      "内层21\n"
     ]
    }
   ],
   "source": [
    "url = r'http://cdn1.python3.vip/files/selenium/sample1.html'\n",
    "wd = webdriver.Chrome(r'chromedriver.exe')\n",
    "wd.implicitly_wait(3)# 智能等待\n",
    "wd.get(url)\n",
    "\n",
    "\n",
    "\n",
    "elements = wd.find_elements(By.CSS_SELECTOR,'#container > div') # 选择 id=\"container\"的 div的子节点\n",
    "for element in elements:\n",
    "    print(element.text)\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "time.sleep(3)\n",
    "wd.quit()   "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 根据指定的属性值来选择元素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\Handuoduo\\AppData\\Local\\Temp\\ipykernel_19620\\3755605446.py:2: DeprecationWarning: executable_path has been deprecated, please pass in a Service object\n",
      "  wd = webdriver.Chrome(r'chromedriver.exe')\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<a href=\"http://www.miitbeian.gov.cn\">苏ICP备88885574号</a>\n"
     ]
    }
   ],
   "source": [
    "url = r'http://cdn1.python3.vip/files/selenium/sample1.html'\n",
    "wd = webdriver.Chrome(r'chromedriver.exe')\n",
    "wd.implicitly_wait(3)# 智能等待\n",
    "wd.get(url)\n",
    "\n",
    "\n",
    "\n",
    "element = wd.find_element(By.CSS_SELECTOR,'[href=\"http://www.miitbeian.gov.cn\"]') # 选择 href=\"http://www.miitbeian.gov.cn\" 的标签 ； 可以只写'[href]' 表示 选择有href属性的元素\n",
    "print(element.get_attribute('outerHTML'))\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "time.sleep(3)\n",
    "wd.quit()   "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 混合用法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\Handuoduo\\AppData\\Local\\Temp\\ipykernel_19620\\241502950.py:2: DeprecationWarning: executable_path has been deprecated, please pass in a Service object\n",
      "  wd = webdriver.Chrome(r'chromedriver.exe')\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<span>土豆</span>\n",
      "<span>洋葱</span>\n",
      "<span>白菜</span>\n"
     ]
    }
   ],
   "source": [
    "url = r'http://cdn1.python3.vip/files/selenium/sample1.html'\n",
    "wd = webdriver.Chrome(r'chromedriver.exe')\n",
    "wd.implicitly_wait(3)# 智能等待\n",
    "wd.get(url)\n",
    "\n",
    "\n",
    "\n",
    "elements = wd.find_elements(By.CSS_SELECTOR,'div[class=\"plant\"] > span') # 选取class=“plant”的div 标签下的 span 元素\n",
    "\n",
    "for element in elements:\n",
    "    print(element.get_attribute('outerHTML'))\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "time.sleep(3)\n",
    "wd.quit()   "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 选择所有 植物 + 动物\n",
    "用 ， （逗号）隔开"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\Handuoduo\\AppData\\Local\\Temp\\ipykernel_19620\\3535565722.py:2: DeprecationWarning: executable_path has been deprecated, please pass in a Service object\n",
      "  wd = webdriver.Chrome(r'chromedriver.exe')\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<span>土豆</span>\n",
      "<span>洋葱</span>\n",
      "<span>白菜</span>\n",
      "<span>狮子</span>\n",
      "<span>老虎</span>\n",
      "<span>山羊</span>\n"
     ]
    }
   ],
   "source": [
    "url = r'http://cdn1.python3.vip/files/selenium/sample1.html'\n",
    "wd = webdriver.Chrome(r'chromedriver.exe')\n",
    "wd.implicitly_wait(3)# 智能等待\n",
    "wd.get(url)\n",
    "\n",
    "\n",
    "\n",
    "elements = wd.find_elements(By.CSS_SELECTOR,'.plant > span , .animal > span') #选择所有 植物+ 动物\n",
    "\n",
    "for element in elements:\n",
    "    print(element.get_attribute('outerHTML'))\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "time.sleep(3)\n",
    "wd.quit()   "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 按次序选择子节点"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "<!DOCTYPE html>\n",
    "<html lang=\"en\">\n",
    "\n",
    "    <head>\n",
    "        <meta charset=\"UTF-8\">\n",
    "        <title>白月黑羽测试网页1b</title>\n",
    "\n",
    "    </head>\n",
    "\n",
    "    <body>\n",
    "        \n",
    "        \n",
    "       <div id='t1'>\n",
    "           <h3> 唐诗 </h3>\n",
    "           <span>李白</span>\n",
    "           <p>静夜思</p>\n",
    "           <span>杜甫</span>\n",
    "           <p>春夜喜雨</p>  \n",
    "            \n",
    "       </div>      \n",
    "\n",
    "        \n",
    "       <div id='t2'>\n",
    "           <h3> 宋词 </h3>\n",
    "           <span>苏轼</span>\n",
    "           <p>赤壁怀古</p>\n",
    "           <p>明月几时有</p>\n",
    "           <p>江城子·乙卯正月二十日夜记梦</p>\n",
    "           <p>蝶恋花·春景</p>\n",
    "           <span>辛弃疾</span>\n",
    "           <p>京口北固亭怀古</p>\n",
    "           <p>青玉案·元夕</p>\n",
    "           <p>西江月·夜行黄沙道中</p>\n",
    "            \n",
    "       </div>             \n",
    "\n",
    "    </body>\n",
    "</html>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- 父元素的第n个子节点 ： nth-child(n)\n",
    "- 父元素的倒数第n个子节点：nth-last-child(n)\n",
    "- 选择span的第1个元素：span:nth-of-type(1)\n",
    "- 选择span的倒数第1个元素：span:nth-last-of-type(1)\n",
    "- 根据兄弟节点选择【相邻兄弟】 h3 + span ：必须是相邻的\n",
    "- 根据兄弟节点选择【不一定相邻】 h3 ~ span ： 只要在h3后面的span 都会被选中"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\Handuoduo\\AppData\\Local\\Temp\\ipykernel_19620\\3621615380.py:2: DeprecationWarning: executable_path has been deprecated, please pass in a Service object\n",
      "  wd = webdriver.Chrome(r'chromedriver.exe')\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<span>李白</span>\n",
      "<span>苏轼</span>\n"
     ]
    }
   ],
   "source": [
    "url = r'http://cdn1.python3.vip/files/selenium/sample1b.html'\n",
    "wd = webdriver.Chrome(r'chromedriver.exe')\n",
    "wd.implicitly_wait(3)# 智能等待\n",
    "wd.get(url)\n",
    "\n",
    "\n",
    "\n",
    "elements = wd.find_elements(By.CSS_SELECTOR,'span:nth-child(2)') #选span标签 且 作为父节点的第二个子节点\n",
    "\n",
    "for element in elements:\n",
    "    print(element.get_attribute('outerHTML'))\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "time.sleep(3)\n",
    "wd.quit()   "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### frame切换\n",
    "- wd.switch_to.frame('frame1') #参数为 id的值或者 name 的值\n",
    "- 如果没有id和name来定位，可以线提取到该元素，再对该元素进行操作"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\Handuoduo\\AppData\\Local\\Temp\\ipykernel_19620\\452339051.py:2: DeprecationWarning: executable_path has been deprecated, please pass in a Service object\n",
      "  wd = webdriver.Chrome(r'chromedriver.exe')\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<div class=\"plant\"><span>土豆</span></div>\n",
      "<div class=\"plant\"><span>洋葱</span></div>\n",
      "<div class=\"plant\"><span>白菜</span></div>\n"
     ]
    }
   ],
   "source": [
    "url = r'http://cdn1.python3.vip/files/selenium/sample2.html'\n",
    "wd = webdriver.Chrome(r'chromedriver.exe')\n",
    "wd.implicitly_wait(5)# 智能等待\n",
    "wd.get(url)\n",
    "\n",
    "# 切换到frame\n",
    "wd.switch_to.frame('frame1')\n",
    "# wd.switch_to.frame(wd.find_element_by_css_selector('[src=\"sample.html\"]'))  #根据标签切换到frame\n",
    "elements = wd.find_elements(By.CSS_SELECTOR,'.plant') #选span标签 且 作为父节点的第二个子节点\n",
    "for element in elements:\n",
    "    print(element.get_attribute('outerHTML'))\n",
    "\n",
    "# 切换到外层\n",
    "wd.switch_to.default_content()\n",
    "wd.find_element(By.ID,'outerbutton').click()\n",
    "wd.find_element(By.ID,'outerbutton').click()\n",
    "wd.find_element(By.ID,'outerbutton').click()\n",
    "\n",
    "time.sleep(3)\n",
    "wd.quit()   "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 切换到新的窗口 【新的标签页】"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\Handuoduo\\AppData\\Local\\Temp\\ipykernel_19620\\635971796.py:2: DeprecationWarning: executable_path has been deprecated, please pass in a Service object\n",
      "  wd = webdriver.Chrome(r'chromedriver.exe')\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "白月黑羽测试网页3\n",
      "必应\n"
     ]
    }
   ],
   "source": [
    "url = r'http://cdn1.python3.vip/files/selenium/sample3.html'\n",
    "wd = webdriver.Chrome(r'chromedriver.exe')\n",
    "wd.implicitly_wait(5)# 智能等待\n",
    "wd.get(url)\n",
    "\n",
    "link = wd.find_element(By.TAG_NAME,'a')\n",
    "link.click()\n",
    "\n",
    "mainhandle = wd.current_window_handle #保存第一个窗口句柄\n",
    "\n",
    "for handle in wd.window_handles:\n",
    "    wd.switch_to.window(handle)\n",
    "    print(wd.title)\n",
    "    if '必应' in wd.title:\n",
    "        break\n",
    "\n",
    "wd.find_element(By.ID,'sb_form_q').send_keys('英雄联盟\\n')\n",
    "\n",
    "wd.switch_to.window(mainhandle) #切回第一个窗口\n",
    "wd.find_element(By.ID,'outerbutton').click()\n",
    "\n",
    "time.sleep(3)\n",
    "wd.quit()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 选择框  "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Radio 框 【单选】"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\Handuoduo\\AppData\\Local\\Temp\\ipykernel_19620\\918663611.py:2: DeprecationWarning: executable_path has been deprecated, please pass in a Service object\n",
      "  wd = webdriver.Chrome(r'chromedriver.exe')\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "当前选中的老师是: 小凯老师\n"
     ]
    }
   ],
   "source": [
    "url = r'http://cdn1.python3.vip/files/selenium/test2.html'\n",
    "wd = webdriver.Chrome(r'chromedriver.exe')\n",
    "wd.implicitly_wait(5)# 智能等待\n",
    "wd.get(url)\n",
    "\n",
    "#打印当前选中的元素\n",
    "element = wd.find_element(By.CSS_SELECTOR,'#s_radio input[checked]')\n",
    "print('当前选中的老师是: ' + element.get_attribute('value'))\n",
    "\n",
    "#点选 小雷老师\n",
    "wd.find_element(By.CSS_SELECTOR,'#s_radio input[value = \"小雷老师\"]').click()\n",
    "\n",
    "time.sleep(3)\n",
    "wd.quit()\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### checkbox 框\n",
    "\n",
    "- 先把选中的反选\n",
    "- 再选自己想要的选项"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\Handuoduo\\AppData\\Local\\Temp\\ipykernel_19620\\2216478267.py:2: DeprecationWarning: executable_path has been deprecated, please pass in a Service object\n",
      "  wd = webdriver.Chrome(r'chromedriver.exe')\n"
     ]
    }
   ],
   "source": [
    "url = r'http://cdn1.python3.vip/files/selenium/test2.html'\n",
    "wd = webdriver.Chrome(r'chromedriver.exe')\n",
    "wd.implicitly_wait(5)# 智能等待\n",
    "wd.get(url)\n",
    "\n",
    "#找出已经选中的\n",
    "elements = wd.find_elements(By.CSS_SELECTOR,'#s_checkbox input[checked]')\n",
    "for element in elements:\n",
    "    element.click()\n",
    "\n",
    "# 再点击小雷老师\n",
    "wd.find_element(By.CSS_SELECTOR,'#s_checkbox input[value = \"小雷老师\"]').click()\n",
    "\n",
    "time.sleep(3)\n",
    "wd.quit()\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### select 框\n",
    "- 选中元素\n",
    "    - select_by_index(1) : 根据次序选择（从1开始）\n",
    "    - select_by_visible_text('选项A')：根据可见文本选择\n",
    "    - select_by_value() ：根据元素value标签值选择\n",
    "    \n",
    "    \n",
    "- 反选元素\n",
    "    - deselect_all(): 取消选中所有元素\n",
    "    - deselect_by_index(1) : 根据次序选择（从1开始）\n",
    "    - deselect_by_visible_text('选项A')：根据可见文本选择\n",
    "    - deselect_by_value() ：根据元素value标签值选择"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\Handuoduo\\AppData\\Local\\Temp\\ipykernel_19620\\3498479608.py:2: DeprecationWarning: executable_path has been deprecated, please pass in a Service object\n",
      "  wd = webdriver.Chrome(r'chromedriver.exe')\n"
     ]
    }
   ],
   "source": [
    "url = r'http://cdn1.python3.vip/files/selenium/test2.html'\n",
    "wd = webdriver.Chrome(r'chromedriver.exe')\n",
    "wd.implicitly_wait(5)# 智能等待\n",
    "wd.get(url)\n",
    "\n",
    "# 创建Select 对象\n",
    "select = Select(wd.find_element(By.CSS_SELECTOR,'#ss_single'))\n",
    "\n",
    "# 选中小雷老师\n",
    "select.select_by_visible_text(\"小雷老师\")\n",
    "\n",
    "time.sleep(3)\n",
    "wd.quit()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Select 多选框"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\Handuoduo\\AppData\\Local\\Temp\\ipykernel_19620\\1879884171.py:2: DeprecationWarning: executable_path has been deprecated, please pass in a Service object\n",
      "  wd = webdriver.Chrome(r'chromedriver.exe')\n"
     ]
    }
   ],
   "source": [
    "url = r'http://cdn1.python3.vip/files/selenium/test2.html'\n",
    "wd = webdriver.Chrome(r'chromedriver.exe')\n",
    "wd.implicitly_wait(5)# 智能等待\n",
    "wd.get(url)\n",
    "\n",
    "# 创建Select 对象\n",
    "select = Select(wd.find_element(By.CSS_SELECTOR,'#ss_multi'))\n",
    "\n",
    "# 取消选择所有\n",
    "select.deselect_all()\n",
    "\n",
    "# 选中小雷老师 和 小凯老师\n",
    "select.select_by_visible_text(\"小雷老师\")\n",
    "select.select_by_visible_text(\"小凯老师\")\n",
    "\n",
    "\n",
    "time.sleep(3)\n",
    "wd.quit()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 其他操作\n",
    "\n",
    "- ActionChains"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- 鼠标右键\n",
    "- 双击\n",
    "- 移动鼠标到某个元素悬停\n",
    "- 鼠标拖拽"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "例：鼠标悬停"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\Handuoduo\\AppData\\Local\\Temp\\ipykernel_19620\\2803019047.py:2: DeprecationWarning: executable_path has been deprecated, please pass in a Service object\n",
      "  wd = webdriver.Chrome(r'chromedriver.exe')\n"
     ]
    }
   ],
   "source": [
    "url = r'https://www.baidu.com/'\n",
    "wd = webdriver.Chrome(r'chromedriver.exe')\n",
    "wd.implicitly_wait(5)# 智能等待\n",
    "wd.get(url)\n",
    "\n",
    "#实例化AC\n",
    "ac = ActionChains(wd)\n",
    "\n",
    "#鼠标悬停\n",
    "ac.move_to_element(wd.find_element(By.CSS_SELECTOR,'[name=\"tj_briicon\"]')).perform() # 执行perform() 才会执行\n",
    "\n",
    "time.sleep(3)\n",
    "wd.quit()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 冻结界面\n",
    "示例网站：[小米官网](https://www.mi.com/)\n",
    " \n",
    " js:\n",
    " \n",
    " `setTimeout(function(){debugger},5000)`\n",
    " \n",
    " 表示在 5000毫秒后，执行 debugger 命令"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 弹出对话框\n",
    "\n",
    "- alert\n",
    "- confim\n",
    "- prompt"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### alert"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\Handuoduo\\AppData\\Local\\Temp\\ipykernel_19620\\767957843.py:2: DeprecationWarning: executable_path has been deprecated, please pass in a Service object\n",
      "  wd = webdriver.Chrome(r'chromedriver.exe')\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "现在开始和白月黑羽一起学Python!\n"
     ]
    }
   ],
   "source": [
    "url = r'http://cdn1.python3.vip/files/selenium/test4.html'\n",
    "wd = webdriver.Chrome(r'chromedriver.exe')\n",
    "wd.implicitly_wait(5)# 智能等待\n",
    "wd.get(url)\n",
    "\n",
    "#点击第一个按钮\n",
    "wd.find_element(By.ID,'b1').click()\n",
    "\n",
    "#打印 弹出对话框提示信息\n",
    "print(wd.switch_to.alert.text)\n",
    "\n",
    "#点击Ok 按钮\n",
    "wd.switch_to.alert.accept()\n",
    "\n",
    "time.sleep(3)\n",
    "wd.quit()\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### confim\n",
    "\n",
    "- 点ok和打印 同上\n",
    "- 点取消：wd.switch_to.alert.dismiss()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### prompt\n",
    "- 点ok、打印、取消 同上\n",
    "- 输入内容：wd.switch_to.alert.send_keys('xxxx')"
   ]
  }
 ],
 "metadata": {
  "interpreter": {
   "hash": "4ce0e62306dd6a5716965d4519ada776f947e6dfc145b604b11307c10277ef29"
  },
  "kernelspec": {
   "display_name": "Python 3.8.9 64-bit",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.9"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
